<template>
  <dt-modal-edit
    ref="edit"
    :title="title"
    :loading="loading"
    :label-width="100"
    okText="保存"
    :width="400"
    @ok="onOk">
    <div class="select_uaser">
      <div style="color: #ffa000; font-size: 12px; line-height: 20px">
        <div>案场码，生成线索会直接推送至案场，可由专案分配</div>
        <div>顾问角色拓客码，生成线索会直接推送给顾问</div>
      </div>
      <dt-grid ref="table" :onFetch="onFetch" :select="select" :height="400" />
    </div>
  </dt-modal-edit>
</template>

<script>
  import columnOpt from '../opt/column'
  import activityMgr from '@/biz/customerForCode/activity'

  export default {
    data() {
      return {
        loading: false,
        title: '选择人员',
        selected: [],
        list: [],
        defaultSelect: [],
        select: {
          checkbox: {
            checkMethod: ({ row }) => {
              let _index = this.defaultSelect.some((item) => {
                return item.userId == row.userId
              })
              if (!_index) {
                return true
              }
            }
          },
          one: (selection, row) => {
            if (selection.some((i) => i.userId == row.userId)) {
              this.selected.push(row)
            } else {
              let idx = this.selected.findIndex((i) => i.userId == row.userId)
              if (idx >= 0) {
                this.selected.splice(idx, 1)
              }
            }
          },
          all: (selection) => {
            let tmp = []
            if (selection.length > 0) {
              selection.forEach((item) => {
                if (!this.selected.some((i) => i.userId == item.userId)) {
                  tmp.push(item)
                }
              })
              this.selected = [...this.selected, ...tmp]
            } else {
              this.list.forEach((item) => {
                let idx = this.selected.findIndex((i, idx) => i.userId == item.userId)
                if (idx >= 0) {
                  this.selected.splice(idx, 1)
                }
              })
            }
          }
        }
      }
    },
    methods: {
      show(data) {
        this.selected = this.$tool.deepClone(data.selectUser)
        this.defaultSelect = data.defaultSelect
        this.$refs.edit.show()
        this.$refs.table.init(this, new columnOpt.ColumnSelectUser(this))
        this.$refs.table.fetch()
      },
      //默认勾选
      findSelected(list) {
        let tmp = []
        list.forEach((item) => {
          if (this.selected.some((i) => i.userId == item.userId)) {
            tmp.push(item)
          }
        })
        this.$refs.table.selectRows(tmp)
      },
      onFetch(data) {
        return activityMgr.adviserList().then((res) => {
          this.list = res
          this.list.unshift({ userName: '案场码', userId: -1 })
          this.findSelected(this.list)
          return res
        })
      },
      onOk() {
        this.$emit('success', this.selected)
        this.selected = []
        this.$refs.edit.hide()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .select_uaser {
    .label_warp {
      margin: 15px 0;
      background-color: rgba(254, 252, 236, 1);
      width: auto;
      line-height: 32px;
      padding: 10px;

      .tips {
        line-height: 20px;
        font-size: 14px;
        display: flex;
        justify-content: center;

        .tips_icon {
          font-size: 20px;
          color: #0f42a7;
          vertical-align: sub;
        }
      }
    }

    .setTips {
      color: #f59a23;
      margin-top: 16px;
      text-align: center;
    }

    ::v-deep .is--disabled .elx-checkbox--unchecked-icon:after {
      box-sizing: content-box;
      content: '';
      border: 1px solid #409eff;
      border-left: 0;
      border-top: 0;
      height: 7px;
      left: 5px;
      position: absolute;
      top: 2px;
      transform: rotate(45deg) scaleY(1);
      width: 3px;
      transition: transform 0.15s ease-in 0.05s;
      transform-origin: center;
    }
  }
</style>
